package com.ysucode.web.websocket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

//@Component
@ServerEndpoint("/doublePkWebSocket/{msg}")

public class DoublePkWebSocket {
    //用户id
    private String id;

    //该用户的对手id
    private String rivalId;

    //每个客户端自己的session
    private Session session;

    //所有进入双人pk页面的用户
    private static ConcurrentHashMap<String, DoublePkWebSocket> concurrentHashMap = new ConcurrentHashMap<>();

    //客户端数量
    private static int onlineCount = 0;

    //标记用户答对的题目 key为题目id value为0代表未答 1代表正确
    private ConcurrentHashMap<Integer, Integer> problemStatus = new ConcurrentHashMap<>();

    //标记用户是否为房主 这里设定id较小的为房主
    private boolean isMain;

    //当前得分
    private int score;

    @OnMessage
    public void onMessage(String message, Session session) throws IOException, InterruptedException {
        //打印客户端消息
        System.out.println("前端传来的信息为:" + message);
        //System.out.println("status：" + status);

        //提交题目的答案正确且对手没有答对这道题时 记录加分
        if (message.indexOf("douSub") != -1) {
            //获取题目id
            String temp = message.substring(6);
            System.out.println("该题目id为:" + temp);

            //对手此题状态为0
            if (concurrentHashMap.get(rivalId).problemStatus.get(Integer.parseInt(temp)) == 0) {
                //分数增加
                score++;
                //标记题目状态为1
                problemStatus.put(Integer.parseInt(temp), 1);
                System.out.println(id + "当前分数为:" + score);
            } else { //对手此题状态为1
                System.out.println("对手已抢答成功");
            }
        }
    }

    @OnOpen
    public void onOpen(@PathParam("msg") String msg, Session session) throws IOException {
        //获取当前用户session
        this.session = session;

        //设置初始分数
        score = 0;

        //获得用户id和对手id
        String[] strings = msg.split("&");
        this.id = strings[0];
        this.rivalId = strings[1];
        System.out.println(id + " " + rivalId);

        //标记id较小的为房主
        if (Integer.parseInt(id) == Math.min(Integer.parseInt(id), Integer.parseInt(rivalId))) {
            isMain = true;
            System.out.println("用户id为:" + id + ",房主为:" + id);
        } else {
            isMain = false;
            System.out.println("用户id为:" + id + ",房主为:" + rivalId);
        }

        //添加用户
        concurrentHashMap.put(id, this);

        //当两人都加入容器中时生成题目id并发送回客户端
        if (concurrentHashMap.containsKey(id) && concurrentHashMap.containsKey(rivalId)) {
            // TODO: 2022/5/7 随机从已有题库里生成5道题目
            //这里先模拟随机生成了五道题
            int[] problems = {10, 10, 51, 10, 10};
            System.out.println(id + ":10,10,51,10,10");

            //把题目id发给客户端
            session.getBasicRemote().sendText("id:10,10,51,10,10");
            concurrentHashMap.get(rivalId).session.getBasicRemote().sendText("id:10,10,51,10,10");

            //服务端记录题目id
            for (int i = 0; i < 5; i++) {
                problemStatus.put(problems[i], 0);
                concurrentHashMap.get(rivalId).problemStatus.put(problems[i], 0);
            }
        }


    }

    @OnClose
    public void onClose(@PathParam("msg") String msg, Session session) {
        System.out.println(id + "已离开!");
    }

    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
        DoublePkWebSocket.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
        DoublePkWebSocket.onlineCount--;
    }

    public static ConcurrentHashMap<String, DoublePkWebSocket> getWebSocketSet() {
        return concurrentHashMap;
    }

    public static void setWebSocketSet(ConcurrentHashMap<String, DoublePkWebSocket> webSocketSet) {
        DoublePkWebSocket.concurrentHashMap = webSocketSet;
    }
}


